package concurrent;

/**
 * synchronized  作用级别是类而不是方法，故此只能作用到静态方法上
 *
 * 同时体现了static变量和方法与普通变量和方法的不同（static修饰的变量和方法是类级别）
 *
 *
 *
 *
 * @author fengpeng
 * @date 2019-05-23  20:35
 */
public class MultiThread {

    private  int temp1 = 0;

    private  synchronized void funciton1(int t) throws InterruptedException {
        if (temp1 == t){
            temp1 = 100;
            System.out.println("t : "+t+" temp:"+temp1);
            Thread.sleep(2000L);
        }else {
            temp1 = 200;
            System.out.println("t : "+t+" temp:"+temp1);
        }
    }

    private static void test1(){
        final MultiThread mt1 = new MultiThread();
        final MultiThread mt2 = new MultiThread();

        Thread t1 = new Thread(new Runnable() {
            public void run() {
                try {
                    mt1.funciton1(0);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        t1.start();

        Thread t2 = new Thread(new Runnable() {
            public void run() {
                try {
                    mt2.funciton1(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        t2.start();
    }

    private static void test2(){
        final MultiThread mt1 = new MultiThread();

        Thread t1 = new Thread(new Runnable() {
            public void run() {
                try {
                    mt1.funciton1(0);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        t1.start();

        Thread t2 = new Thread(new Runnable() {
            public void run() {
                try {
                    mt1.funciton1(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        t2.start();
    }
    private static final String GUARANTEE = "_GUARANTEE";

    public static void main(String[] args){
        //test2();
      System.out.println("REVEAL_ALL_THE_DETAILS".endsWith(GUARANTEE));
    }
}
